package config;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @description: 连接 RabbitMqClient
 * @author:刘腾辉
 * @create:2020/10/20 9:22
 */
public class RabbitMqConnection {
    public static Connection getRabbitMqConnection(){

        /**
         * 1.创建连接工厂
         */
        ConnectionFactory connectionFactory = new ConnectionFactory();

        /**
         * 2.设置相关配置：
         * setHost：连接地址
         * setVirtualHost：虚拟机
         * setPort：端口
         * setUsername：用户名
         * setPassword：密码
         */
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setVirtualHost("/test");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("test");
        connectionFactory.setPassword("test");

        /**
         * 3.获取连接
         */
        Connection connection = null;
        try {
            connection = connectionFactory.newConnection();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }

        return connection;
    }
}
